package com.example.demo.common;

import lombok.Data;

import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * Description: 普通对象，作用是统一返回数据格式
 */
@Data
public class HTTPResult implements Serializable {
    // 状态码
    private Integer code;
    // 状态码描述信息
    private String msg;
    // 返回的数据
    private Object data;

    /**
     * 成功执行的返回结果
     */
    public static HTTPResult success(Object data) {
        HTTPResult result = new HTTPResult();
        result.setCode(200);
        result.setMsg("");
        result.setData(data);
        return result;
    }
    public static HTTPResult success(Integer code, Object data) {
        HTTPResult result = new HTTPResult();
        result.setCode(code);
        result.setMsg("");
        result.setData(data);
        return result;
    }
    public static HTTPResult success(Integer code, String msg, Object data) {
        HTTPResult result = new HTTPResult();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    /**
     * 异常返回
     */
    public static HTTPResult fail(Integer code, String msg) {
        HTTPResult result = new HTTPResult();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(null);
        return result;
    }

    public static HTTPResult fail(Integer code, String msg, Object data) {
        HTTPResult result = new HTTPResult();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }
}
